import 'package:inmotion_pro/pages/models/base_model.dart';
import 'package:inmotion_pro/pages/models/firmware_info_model.dart';
import 'package:inmotion_pro/utils/utils.dart';
import 'package:flutter/foundation.dart';
import 'package:safemap/safemap.dart';

// 产品信息模型
class ProductInfoModel extends BaseModel {
  // 数据结构格式，从1开始
  int dataFormat = 1;

  // 接口类型
  int interfaceType = 0;

  // 车辆sn
  String sn = '';

  // 主系列
  int mainSeries = 0;

  // 子系列
  int subSeries = 0;

  // 具体型号
  int model = 0;

  //批次特征
  int batch = 0;

  // 固件信息
  List<FirmwareInfoModel> firmwareInfoList = [];

  // 产品型号
  String get productModel => Utils.uint8List2HexString(
        Uint8List.fromList([mainSeries, subSeries, model, batch, 0x00, 0x00]),
      );

  // 激活日期
  DateTime activationDate = DateTime.now();

  ProductInfoModel.fromMap(Map<String, dynamic> map) {
    SafeMap safeMap = SafeMap(map);
    dataFormat = safeMap['data_format'].toInt ?? 1;
    interfaceType = safeMap['interface_type'].toInt ?? 0;
    sn = safeMap['sn'].string ?? '';
    mainSeries =
        safeMap['main_series'].toInt ?? safeMap['mainSeries'].toInt ?? 0;
    subSeries = safeMap['sub_series'].toInt ?? safeMap['subSeries'].toInt ?? 0;
    model = safeMap['model'].toInt ?? 0;
    batch = safeMap['batch'].toInt ?? safeMap['feature'].toInt ?? 0;
    firmwareInfoList = safeMap['firmware_info']
            .list
            ?.map<FirmwareInfoModel>((item) => FirmwareInfoModel.fromMap(item))
            .toList() ??
        [];

    activationDate =
        DateTime.tryParse(safeMap['activation_date'].string ?? '') ??
            DateTime.now();
  }

  @override
  Map<String, dynamic> toMap() {
    return {
      'data_format': dataFormat,
      'interface_type': interfaceType,
      'sn': sn,
      'main_series': mainSeries,
      'sub_series': subSeries,
      'model': model,
      'batch': batch,
      'product_model': productModel,
      'activation_date': activationDate.toIso8601String(),
      'firmware_info': firmwareInfoList.map((e) => e.toMap()).toList(),
    };
  }
}
